function addGoods() {
	var price = document.getElementById("price");
	var notInStock = document.getElementById("notInStock");
	if (notInStock.checked) {
		price.value = "";
		price.disabled = true;
		price.style.backgroundColor = "#dddddd";
	} else {
		price.value = "";
		price.disabled = false;
		price.style.backgroundColor = "#ffffff";
	}
}

function goodsCheckbox(checkbox) {
	var price = checkbox.parentNode.previousSibling.firstChild;
	if (checkbox.checked) {
		price.value = "";
		price.disabled = true;
		price.style.backgroundColor = "#dddddd";
	} else {
		price.value = "";
		price.disabled = false;
		price.style.backgroundColor = "#ffffff";
	}
}

var modelPattern = "^[A-Za-z]{2}[0-9]{3}$";
var datePattern = "^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)\\d\\d$";
var pricePattern = "^[\\d]+[.]?[\\d]+$";

function checkData(form) {
	var length = form.length;
	var j = 0;
	var validForm = true;
	for ( var i = 0; i < length; i++) {
		element = form[i];
		if (element.type == "text") {
			value = element.value;
			if (j == 5) {
				j = 0;
			}
			switch (j) {
			case 0:
				if (value.length == 0) {
					valid = false;
				} else {
					valid = true;
				}
				break;
			case 1:
				valid = checkValuePattern(value, modelPattern);
				break;
			case 2:
				valid = checkValuePattern(value, datePattern);
				break;
			case 3:
				if (value.length == 0) {
					valid = false;
				} else {
					valid = true;
				}
				break;
			case 4:
				if (element == null) {
					valid = true;
				} else {
					var notInStock = element.parentNode.nextSibling.firstChild;
					if(notInStock.checked){
						valid = true;
					} else {
						valid = checkPrice(value);
					}
				}
				break;
			default:
				break;
			}
			setBackground(valid, element);
			if (!valid) {
				validForm = false;
			}
			j++;
		}
	}
	if (!validForm) {
		alert("Incorrect data was entered");
	}
	return validForm;
}

function checkPrice(value) {
	if (value.length == 0) {
		return false;
	} else {
		return checkValuePattern(value, pricePattern);
	}
}

function setBackground(valid, element) {
	if (!valid) {
		element.style.backgroundColor = "#CD5C5C";
	} else {
		element.style.backgroundColor = "#FFFFFF";
	}
}

function checkValuePattern(value, pattern) {
	var regExp = new RegExp(pattern);
	return regExp.test(value);
}
